/*
*  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
*  reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of source code must retain the above copyright
*  notice, this list of conditions and the following disclaimer.
*
*  2. Redistributions in binary form must reproduce the above copyright
*  notice, this list of conditions and the following disclaimer in
*  the documentation and/or other materials provided with the
*  distribution.
*
*  3. The end-user documentation included with the redistribution,
*  if any, must include the following acknowledgment:
*  "This product includes software developed by the
*  Sun Microsystems, Inc. for Project JXTA."
*  Alternately, this acknowledgment may appear in the software itself,
*  if and wherever such third-party acknowledgments normally appear.
*
*  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
*  must not be used to endorse or promote products derived from this
*  software without prior written permission. For written
*  permission, please contact Project JXTA at http://www.jxta.org.
*
*  5. Products derived from this software may not be called "JXTA",
*  nor may "JXTA" appear in their name, without prior written
*  permission of Sun.
*
*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
*  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*  SUCH DAMAGE.
*  ====================================================================
*
*  This software consists of voluntary contributions made by many
*  individuals on behalf of Project JXTA.  For more
*  information on Project JXTA, please see
*  <http://www.jxta.org/>.
*
*  This license is based on the BSD license adopted by the Apache Foundation.
*
*  $Id: AbstractSearch.java,v 1.5 2006/06/06 18:46:23 nano Exp $
*/
package net.jxta.myjxta.search;

import net.jxta.discovery.DiscoveryService;
import net.jxta.myjxta.MyJXTA;
import net.jxta.myjxta.util.Group;
import net.jxta.myjxta.util.objectmodel.JxtaNode;

import java.util.*;

/**
 * @author james todd [gonzo at jxta dot org]
 * @version $Id: AbstractSearch.java,v 1.5 2006/06/06 18:46:23 nano Exp $
 */

public abstract class AbstractSearch
        implements Search {

    /**
     * The wildcard character to use
     */
    protected static final String WILDCARD = SearchModifier.WILDCARD;

    /**
     * The threshold on the number of returned results
     */
    protected static final int REMOTE_THRESHOLD = 10;


    /**
     * The group in which context to do the search
     */
    protected Group group = null;

    /**
     * The MyJXTA instance for system wide defaults
     */
    protected final MyJXTA myJxta;

    /**
     * The search term
     */
    private String term = null;

    /**
     * The title of this search
     */
    private String title = null;

    /**
     * The list of registered listeners
     */
    private Set<SearchListener> listeners = null;

    /**
     * Is the search currently running
     */
    private boolean isRunning = false;

    /**
     * Create a new Search object
     *
     * @param group  the group in which this search is done
     * @param myJxta the MyJxta instance for system wide defaults.
     *               The currently selected group in this instance my be different
     *               from group
     */
    public AbstractSearch(Group group, MyJXTA myJxta) {
        this.group = group;
        this.myJxta = myJxta;
    }


    /**
     * Get the title of this search
     *
     * @return the title of this search
     */
    public String getTitle() {
        return this.title;
    }

    /**
     * Set the title of the search
     *
     * @param title the new title of the search
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * Create a description for the search. If the search term is
     * empty the passed in description string is printed
     *
     * @param d the string on which to base the description
     * @return a string describing the search
     */
    public String getDescription(String d) {
        String t = getTerm();

        return "Searching for \"" + (t.trim().length() == 0 ? d : "") + t;
    }

    /**
     * Terminate this search
     */
    public void cancel() {
        for (Iterator l = getListeners(); l.hasNext();) {
            ((SearchListener) l.next()).terminate();
        }
    }

    /**
     * Return the search term we ar currently searching for
     *
     * @return the search term
     */
    public String getTerm() {
        return this.term;
    }

    /**
     * Set the string  to search for
     *
     * @param term the new search term
     */
    public void setTerm(String term) {
        this.term = term;
    }

    /**
     * Add a new SearchListner
     *
     * @param listener the SearchListner to add
     */
    public synchronized void addListener(SearchListener listener) {
        if (this.listeners == null ||
                ! this.listeners.contains(listener)) {
            if (this.listeners == null) {
                this.listeners = new HashSet<SearchListener>();
            }

            this.listeners.add(listener);
        }
    }

    /**
     * Remove a previously registered SearchListener
     *
     * @param listener the SearchListener to remove
     */
    public synchronized void removeListener(SearchListener listener) {
        if (this.listeners != null) {
            this.listeners.remove(listener);
        }
    }

    /**
     * Add a new JxtaNode to the SearchListener instance
     * registered as listeners
     *
     * @param node the JxtaNode to add
     */
    public void add(JxtaNode node) {
        for (Iterator l = getListeners(); l.hasNext();) {
            ((SearchListener) l.next()).add(node);
        }
    }

    /**
     * Add a new list of JxtaNode instances to the list of
     * registered SearchListener object
     *
     * @param nodes the list of nodes to add
     */
    public void add(List nodes) {
        for (Iterator l = nodes.iterator(); l.hasNext();) {
            add((JxtaNode) l.next());
        }
    }

    /**
     * Is the search currently running
     *
     * @return true if the search is currently running, false otherwise
     */
    public boolean isRunning() {
        return this.isRunning;
    }

    /**
     * Set the running status of this search
     *
     * @param isRunning the new running status of this search
     */
    public void isRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }

    /**
     * Get the group in which context to do the search.
     * Once called this set the local instance of the group
     * and should thus be called once upon start of search and
     * then the processing of the search result should use the
     * internal group instance.
     *
     * @return the group in which context to do the search
     */
    protected Group getGroup() {
        return this.group;
    }

    /**
     * Get the DiscoveryService in which to do the search
     *
     * @return the DiscoveryService to use
     */
    protected DiscoveryService getDiscovery() {
        if (getGroup() == null || getGroup().getPeerGroup() == null)
            return null;
        return getGroup().getPeerGroup().getDiscoveryService();
    }

    /**
     * Return an iteration of the registered SearchListeners
     *
     * @return an iteration of the registered SearchListeners
     */
    protected synchronized Iterator getListeners() {
        return (this.listeners != null ?
                this.listeners.iterator() : Collections.EMPTY_SET.iterator());
    }
}
